/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.rtx.core.net;

import com.google.common.collect.Maps;
import com.sun.deploy.net.HttpRequest;
import im.rtx.base.event.Event;
import im.rtx.base.event.EventDispatcher;
import im.rtx.core.net.proxy.ProxyInfo;
import im.rtx.core.packet.base.RawPacket;
import im.rtx.core.packet.base.RtxPacket;
import im.rtx.core.utils.Crypter;
import sun.rmi.transport.tcp.TCPTransport;

import java.util.Deque;
import java.util.EventListener;
import java.util.Map;

/**
 * Project  : rtx
 * Author   : solosky < solosky772@qq.com >
 * Created  : 11/22/14
 * License  : Apache License 2.0
 */
public class TransportPool {
    private TcfTransportFactory tcfTransportFactory;
    private HttpTransportFactory httpTransportFactory;
    private EventDispatcher eventDispatcher;

    private Map<String, TransportEntry> transportMap = Maps.newHashMap();


    public void connectTcf(String portName, String host, short port, ProxyInfo proxyInfo){
        TcfTransport tcfTransport = tcfTransportFactory.create(portName, host, port,
                new TcfTransferListener(portName), proxyInfo);
        tcfTransport.connect();
    }


    public void sendPacket(String portName, RtxPacket rtxPacket){
        TransportEntry tcfTransportEntry = transportMap.get(portName);
        TcfTransport tcfTransport = (TcfTransport) tcfTransportEntry.transport;
        if(tcfTransport == null){
            throw new IllegalArgumentException();
        }
        if(tcfTransport.isClosed()){
            throw new IllegalArgumentException();
        }
        if(!tcfTransport.isConnected()){
            tcfTransportEntry.sendQueue.addFirst(rtxPacket);
        }else{
            tcfTransport.send(rtxPacket);
            if(rtxPacket.getControl().isNeedAck()){
                tcfTransportEntry.ackQueue.addFirst(rtxPacket);
            }
        }


    }

    public void sendHttp(String portName, HttpRequest httpRequest){

    }


    private static class TransportEntry{
        public Transport transport;
        public Deque<RtxPacket> sendQueue;
        public Deque<RtxPacket> ackQueue;
        public Crypter crypter;
    }

    private class TcfTransferListener implements TcfListener{

        private String portName;


        public TcfTransferListener(String portName) {
            this.portName = portName;
        }

        @Override
        public void onPacket(RawPacket packet) {

        }

        @Override
        public void onConnected() {
            eventDispatcher.dispatchEvent(new Event("PortConnected", portName));
        }

        @Override
        public void onClosed() {
            eventDispatcher.dispatchEvent(new Event("PortClosed", portName));
        }

        @Override
        public void onError() {
            eventDispatcher.dispatchEvent(new Event("PortError", portName));
        }
    }
}
